import 'package:audioplayers/audioplayers.dart';

import 'types.dart';

class MyAudioPlayer {
  final _player = AudioPlayer();

  MyPlayerState get state {
    switch (_player.state) {
      case PlayerState.playing:
        return MyPlayerState.playing;
      case PlayerState.paused:
        return MyPlayerState.paused;
      case PlayerState.stopped:
      case PlayerState.completed:
      case PlayerState.disposed:
        return MyPlayerState.stopped;
    }
  }

  Future<void> initAssets(String url) async {
    // 从 url 中删除前面 的 assets/ 部分，因为 audioplayers 会自动加入这部分
    if (url.startsWith('assets/')) {
      url = url.substring(7);
    }
    await _player.setSource(AssetSource(url));
    await pause();
  }

  Future<void> pause() {
    return _player.pause();
  }

  Future<void> resume() {
    return _player.resume();
  }

  Future<void> stop() {
    return _player.stop();
  }

  Future<void> seek(Duration position) {
    return _player.seek(position);
  }

  Future<void> dispose() {
    return _player.dispose();
  }
}
